package org.jeecg.modules.demo.ch.service.impl;

import org.jeecg.modules.demo.ch.entity.ChMaterialCodeRawMaterial;
import org.jeecg.modules.demo.ch.entity.ChRawMaterialCodeComparison;
import org.jeecg.modules.demo.ch.mapper.ChRawMaterialCodeComparisonMapper;
import org.jeecg.modules.demo.ch.mapper.ChMaterialCodeRawMaterialMapper;
import org.jeecg.modules.demo.ch.service.IChMaterialCodeRawMaterialService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 物料编码原材料类表
 * @Author: jeecg-boot
 * @Date:   2022-09-16
 * @Version: V1.0
 */
@Service
public class ChMaterialCodeRawMaterialServiceImpl extends ServiceImpl<ChMaterialCodeRawMaterialMapper, ChMaterialCodeRawMaterial> implements IChMaterialCodeRawMaterialService {

	@Autowired
	private ChMaterialCodeRawMaterialMapper chMaterialCodeRawMaterialMapper;
	@Autowired
	private ChRawMaterialCodeComparisonMapper chRawMaterialCodeComparisonMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ChMaterialCodeRawMaterial chMaterialCodeRawMaterial, List<ChRawMaterialCodeComparison> chRawMaterialCodeComparisonList) {
		chMaterialCodeRawMaterialMapper.insert(chMaterialCodeRawMaterial);
		if(chRawMaterialCodeComparisonList!=null && chRawMaterialCodeComparisonList.size()>0) {
			for(ChRawMaterialCodeComparison entity:chRawMaterialCodeComparisonList) {
				//外键设置
				entity.setChMaterialCodeRawMaterialId(chMaterialCodeRawMaterial.getId());
				chRawMaterialCodeComparisonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ChMaterialCodeRawMaterial chMaterialCodeRawMaterial,List<ChRawMaterialCodeComparison> chRawMaterialCodeComparisonList) {
		chMaterialCodeRawMaterialMapper.updateById(chMaterialCodeRawMaterial);
		
		//1.先删除子表数据
		chRawMaterialCodeComparisonMapper.deleteByMainId(chMaterialCodeRawMaterial.getId());
		
		//2.子表数据重新插入
		if(chRawMaterialCodeComparisonList!=null && chRawMaterialCodeComparisonList.size()>0) {
			for(ChRawMaterialCodeComparison entity:chRawMaterialCodeComparisonList) {
				//外键设置
				entity.setChMaterialCodeRawMaterialId(chMaterialCodeRawMaterial.getId());
				chRawMaterialCodeComparisonMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		chRawMaterialCodeComparisonMapper.deleteByMainId(id);
		chMaterialCodeRawMaterialMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chRawMaterialCodeComparisonMapper.deleteByMainId(id.toString());
			chMaterialCodeRawMaterialMapper.deleteById(id);
		}
	}
	
}
